package xyc.sjxy.dao.impl;

import xyc.sjxy.dao.BaseDao;
import xyc.sjxy.dao.UserDao;
import xyc.sjxy.pojo.Users;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao<Users> implements UserDao {
    @Override
    public Users findById(Integer id) throws SQLException {
        return this.selectOne("select * from users where id = ?", id);
    }

    @Override
    public Users findByUsername(String username) throws SQLException {
        return this.selectOne("select * from user where username = ? ", username);
    }

    @Override
    public Users findByUsername(String username, String password) throws SQLException {

        return this.selectOne("select * from user where username = ? and password = ?", username, password);
    }

    @Override
    public List<Users> findAll() {
        return null;
    }

    @Override
    public int insert(Users users) throws SQLException {

        String sql="insert into user(username,password) values(?,?)";
        return this.update(sql, users.getUsername(), users.getPassword());
    }

    @Override
    public int update(Users users) throws SQLException {
        //更新数据库中的名字和邮箱
        String sql = "UPDATE user SET nickname = ?, email = ? WHERE id = ?;";
        return this.update(sql,users.getNickname(), users.getEmail(),users.getId());
    }
    public int updatePwd(Users users) throws SQLException {
        //更新数据库中的密码
        String sql = "UPDATE user SET password = ? WHERE id = ?;";
        return this.update(sql,users.getNew_pwd(),users.getId());
    }

    public int update_pic(Users users) throws SQLException {
        //更新数据库中的头像
        String sql = "UPDATE user SET user_pic = ? WHERE id = ?";
        return this.update(sql,users.getUser_pic(),users.getId());
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int delete(String username) {
        return 0;
    }
}
